package com.st.mall.product.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.st.mall.product.mapper.IProductMapper;
import com.st.mall.product.model.CateModel;
import com.st.mall.product.model.ProductModel;
import com.st.mall.product.service.IProductService;

@Service
public class ProductServiceImpl implements IProductService{

	@Autowired
	private IProductMapper mapper;

	@Override
	public String insert(ProductModel productModel) {
		String code = productModel.getCode();
		ProductModel model1 = mapper.selectId(code);	
		if(model1 != null) {
			return "A";		// 编号重复，该一级分类已存在
		}
		int result = mapper.insert(productModel);
		if(result == 1) {
			return "B";		// 添加成功
		}
		return "C";
	}

	@Override
	public List<ProductModel> selecctModel(ProductModel productModel) {
		List<ProductModel> list = mapper.selectModel(productModel);
		return list;
	}

	@Override
	public int selectCount(ProductModel productModel) {
		int result = mapper.selectCount(productModel);
		return result;
	}

	@Override
	public ProductModel selectId(String code) {
		ProductModel result = mapper.selectId(code);
		return result;
	}
	
	@Override
	public ProductModel selectName(String name) {
		name = "%"+name+"%";
		if(name.equals("%%"))
			return null;
		List<ProductModel> list = new ArrayList<>();
		list = mapper.selectName(name);
		return list.get(0);
	}

	@Override
	public int update(ProductModel productModel) {
		int result = mapper.update(productModel);
		return result;
	}

	@Override
	public int delete(String code) {
		int result = mapper.delete(code);
		return result;
		
	}

	@Override
	public int updateLev(ProductModel productModel) {
		int result = mapper.updateActive(productModel);
		return result;
	}
}
